import axios from '../http'
import utils from './utils'
export function
    cheeToken() { // 输出
    var flag = null;
    console.log('进入cheeToken...');
    var toToken = null;
    if (utils.getUrlKey('token')) {
        toToken = utils.getUrlKey('token');
        localStorage.removeItem('token')
        console.log("url有token")
        //return 888;
    }
    //toToken==null 等于 !toToken
    if (!toToken && localStorage.getItem('token')) {
        console.log("url没没没没有token，拿local的token")
        toToken = localStorage.getItem('token')
        //return 888;
    }
    
    if (toToken) {
        console.log("toToken===" + toToken)
        
/*
axios.get('/api/checkTokenCookie2', {
            params: {
                'token': toToken,
            }
        })
*/
       
        flag=axios({
            methods:'get',
            url:'/api/checkTokenCookie2',
            headers:{
                 
                 'UserToken': toToken
            },
            params: {
                token: toToken,
            }
        }).then((res) => {
            
            if (res.data.code == "200") {
                console.log("成功。"+res.data.code+"---"+res.data.message);
                localStorage.setItem('token', toToken);
                //cookie保存15天
                //if(res.data.result.user_name){
                 //   utils.setCookie("nickname",res.data.result.user_name,1296000)
                //}
                if(res.data.result){
                    if(res.data.result.cookies){
                        var cookiess=res.data.result.cookies;
                        console.log("进入cookies...循环。。。")
                        console.log(cookiess)
                        for(let k in cookiess) {  
                            console.log(k,cookiess[k]);  
                            utils.setCookie(k,cookiess[k],1296000)
                        }
                        //utils.setCookie("nickname",res.data.result.user_name,1296000)
                    }
                }
                return Promise.resolve("ok")
                //return "ok";
            } else if(res.data.code == "500"){
                return Promise.resolve("service_error")
            }else {
                console.log("失败===res.data.code过期");
                return Promise.resolve("no")
            }
        }).catch((err) => {
            //Message.error(response.data.message);
            //return Promise.reject(err);
            console.log("carch Error 请求失败。。。");
            return Promise.reject("no");
        })
    } else {
        flag=Promise.resolve("no");
    }
    return flag;
}
export function pureCheckToken(){
    //此方法专门给非ready准备的
    var flag = null;
    console.log('进入pureCheckToken...');
    var toToken = null;
    if (localStorage.getItem('token')) {
        toToken = localStorage.getItem('token')
        //return 888;
    }
    console.log("toToken===" + toToken)
    if (toToken) {
        //debugger;
        flag=axios.get('/api/pureCheckToken').then((res) => {
            if (res.data.code && res.data.code == "200") {
                console.log("message是"+res.data.message);
                return Promise.resolve("ok")
                //return "ok";
            }else if(res.data.code == "500"){
               
                return Promise.resolve("service_error");
            }else{
                
                console.log("不等于200...");
                return Promise.resolve("no");
            }
        }).catch((err) => {
            //Message.error(response.data.message);
            //return Promise.reject(err);
            console.log("carch Error 请求失败。。。");
            return Promise.resolve("no");
        })
    } else {
        flag=Promise.resolve("no");
    }
    return flag;
}
